using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class PhanHoiRepository
	{
		public List<PhanHoi> SelectAll()
		{
			return DataContext.Instance.PhanHois.ToList();
		}

		public PhanHoi SelectByID(int id)
		{
			return DataContext.Instance.PhanHois.FirstOrDefault(item => item.ID == id );
		}

		public List<PhanHoi> SelectPaging(int page, int pageSize)
		{
			var lstItem = DataContext.Instance.PhanHois.ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(PhanHoi obj)
		{
			try
			{
				DataContext.Instance.PhanHois.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.PhanHois.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.PhanHois.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.PhanHois.Count();
		}

		public int GetTotalItemByHopThu(int idhopthu)
		{
			return (from p in DataContext.Instance.PhanHois where p.IDHopThu == idhopthu select p).Count();
		}

		public int GetTotalItemByNguoiPhanHoi(Guid idnguoiphanhoi)
		{
			return (from p in DataContext.Instance.PhanHois where p.IDNguoiPhanHoi == idnguoiphanhoi select p).Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByHopThu(int idhopthu)
		{
			int RecordCount = GetTotalItemByHopThu(idhopthu);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByNguoiPhanHoi(Guid idnguoiphanhoi)
		{
			int RecordCount = GetTotalItemByNguoiPhanHoi(idnguoiphanhoi);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<PhanHoi> RetrieveByID(int id)
		{
			var lst = (from item in DataContext.Instance.PhanHois where  item.ID == id  select item).ToList();
			var lstItem = new List<PhanHoi>();
			lstItem.Add(lst[0]);
			return lstItem;
		}

		public List<PhanHoi> SelectByHopThu(int idhopthu, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.PhanHois where item.IDHopThu == idhopthu select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public List<PhanHoi> SelectByNguoiPhanHoi(Guid idnguoiphanhoi, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.PhanHois where item.IDNguoiPhanHoi == idnguoiphanhoi select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

	}
}